package com.example.materialscancode.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

/**
 * Created by D&LL on 2016/9/20.
 * 一些工具类
 */
public class Tools {
    public static final String FILE_NAME = "string_data";

    /**
     * SharedPreferences 保存String数据
     */
    public static void put_datas(Context context, String name, String data) {
        SharedPreferences preferences = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(name, data);
        editor.commit();
    }

    /**
     * SharedPreferences 获取String数据
     */
    public static String get_datas(Context context, String name) {
        SharedPreferences preferences = context.getSharedPreferences(FILE_NAME,
                Context.MODE_PRIVATE);
        String data = preferences.getString(name, null);
        return data;
    }

    /**
     * 改变字体的颜色
     *
     * @return
     */
    public static Spannable change_world_color(CharSequence text, int color, int begin, int end) {
        Spannable span = new SpannableString(text);
        span.setSpan(new ForegroundColorSpan(color), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return span;
    }

    /**
     * 把long型数据转换为string
     * @param n
     * @return
     */
    public static String long2string(long n) {
        return String.valueOf(n);
    }

    /**
     * 把string型转换为long
     * @param s
     * @return
     */
    public static long string2long(String s) {
        return Long.parseLong(s);
    }

}